package com.urun.interceptor.hotanalysis.controller;

import com.alibaba.fastjson.JSONObject;
import com.urun.interceptor.hotanalysis.DTO.StWordHiddenDTO;
import com.urun.interceptor.hotanalysis.DTO.StWordSearchDTO;
import com.urun.interceptor.hotanalysis.DTO.StWordSortDTO;
import com.urun.interceptor.hotanalysis.bean.Result;
import com.urun.interceptor.hotanalysis.bean.StWords;
import com.urun.interceptor.hotanalysis.service.StWordsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * 舆论热词功能
 * @author zqz
 * @date 2022/3/31 17:46
 */

@RestController
@RequestMapping("/Word")
@CrossOrigin(origins = "*",maxAge = 3600)
@Api(tags = "舆论热词")
public class StWordController {

    @Autowired
    StWordsService stWordsService;

    @ApiOperation(value = "搜索显示当前热词(显示20个)",notes = "通过status字段判断是否是当前热词还是屏蔽热词；返回字段为id,status,frequency,realWord;请求方式:GET,支持格式:application/json")
    @GetMapping("/searchWord")
    public Result SearchWordDTO(){
        Result result = new Result();
        List<StWordSearchDTO> moduleSearchList = stWordsService.SearchWordDTO();
        if (moduleSearchList.isEmpty()){
            return new Result("404","查询不到数据",null);
        }else{
            result.setMessage("查询成功");
            result.setCode("200");
            result.setData(moduleSearchList);
        }
        return result;
    }

    @ApiOperation(value = "搜索显示屏蔽热词",notes = "通过status字段判断是否是当前热词还是屏蔽热词;请求方式:GET,支持格式:application/json")
    @GetMapping("/SearchHiddenWordDTO")
    public Result SearchHiddenWordDTO(){
        Result result = new Result();
        List<StWordSearchDTO> moduleSearchList = stWordsService.SearchHiddenWordDTO();
        result.setMessage("查询成功");
        result.setCode("200");
        result.setData(moduleSearchList);
        return result;
    }

    @ApiOperation(value = "搜索显示热词排行榜",notes = "通过词频大小来排序关键字(修改关键字花名不会影响词频排序);请求方式:GET,支持格式:application/json")
    @GetMapping("/SearchWordDTO")
    public Result SortFrequency(){
        Result result = new Result();
        List<StWordSortDTO> sort = stWordsService.sort();
        if (sort.isEmpty()){
            return new Result("404","查询不到数据",null);
        }else {
            result.setMessage("查询成功");
            result.setCode("200");
            result.setData(sort);
        }
        return result;
    }

    @ApiOperation(value = "更新status的值",notes = "如果status为1就会更新为2,为2就会更新为1;请求方式:POST,支持格式:application/json")
    @PostMapping("UpdateStatus")
    public Result UpdateStatus(@RequestBody JSONObject obj){
        Integer id = obj.getInteger("id");
        boolean b = stWordsService.UpdateStatus(id);
        if (!b){
          new Result("404","查询不到对应的数据进行修改",null);
        }
        return new Result("200","修改成功",null);
    }

    @ApiOperation(value = "恢复默认设置",notes = "更新status的值全为1;请求方式:POST,支持格式:application/json")
    @PostMapping("UpdateDefaultStatus")
    public Result UpdateDefaultStatus(){
        boolean b = stWordsService.UpdateDefaultStatus();
        if (!b){
            new Result("404","恢复默认失败",null);
        }
        return new Result("200","修改成功",null);

    }

    @ApiOperation(value = "修改关键词",notes = "在原来的关键词的基础上添加花名;请求方式:POST,支持格式:application/json")
    @PostMapping("UpdateNewWord")
    public Result UpdateNewWord(@RequestBody JSONObject obj){
        String newWord = obj.getString("newWord");
        Integer id = obj.getInteger("id");
        boolean b = stWordsService.StNewWordEdit(newWord,id);
        if (!b){
            new Result("404","修改失败",null);
        }
        return new Result("200","修改成功",null);

    }

    @ApiOperation(value = "通过Id的值来找到关键字",notes = ";请求方式:POST,支持格式:application/json")
    @PostMapping("selectWordById")
    public Result selectWordById(@RequestBody JSONObject obj){
        Result result = new Result();
        Integer id = obj.getInteger("id");
        List<StWords> stWords = stWordsService.find(id);
        if (stWords.isEmpty()){
            return new Result("404","查询不到数据",null);
        }else{
            result.setMessage("查询成功");
            result.setCode("200");
            result.setData(stWords);
        }
        return result;
    }

    @GetMapping("/search")
    public Result search(){
        Result result = new Result();
        List<StWordHiddenDTO> list = new ArrayList<>();
        List<StWordHiddenDTO> search = stWordsService.search();
        for (StWordHiddenDTO dto :
                search) {
            if (dto.getNewWord()==null|| dto.getNewWord().equals("")){
                dto.setNewWord(dto.getWord());
            }
            list.add(dto);
        }

        result.setMessage("查询成功");
        result.setCode("200");
        result.setData(list);

        return result;

    }


}
